package com.hfjyz.judge.judge.dao;

import java.util.List;

import com.hfjyz.base.bean.SearchCondition;
import com.hfjyz.base.dao.BaseDao;
import com.hfjyz.base.dao.PagingHibernateCallback;
import com.hfjyz.base.entity.JudgeEntity;
import com.hfjyz.judge.judge.bean.JudgeSearchBean;

public class JudgeDao extends BaseDao<JudgeEntity> {

    public JudgeDao() {
        super(JudgeEntity.class);
    }

    @SuppressWarnings("unchecked")
    public long countJudge(JudgeSearchBean judgeSearch) {
        String hql = "select count(judge.id) from JudgeEntity as judge";

        Object[] values = null;
        if (judgeSearch != null) {
            hql += judgeSearch.getWhereClause();
            List<Object> paramList = judgeSearch.getParamList();
            values = paramList.toArray();
        }
        List<Object> result = hibernateTemplate.find(hql, values);

        return (Long) result.get(0);
    }

    @SuppressWarnings("unchecked")
    public List<JudgeEntity> findJudge(SearchCondition cond, JudgeSearchBean judgeSearch) {
        String hql = "from JudgeEntity ";

        Object[] values = null;
        if (judgeSearch != null) {
            hql += judgeSearch.getWhereClause();
            List<Object> paramList = judgeSearch.getParamList();
            values = paramList.toArray();
        }
        hql += cond.getOrderString();

        PagingHibernateCallback callback = new PagingHibernateCallback(hql, cond.getPageNum(),
                cond.getRowCountPerPage(), values);
        List<JudgeEntity> result = hibernateTemplate.executeFind(callback);
        return result;
    }

    @SuppressWarnings("unchecked")
    public List<JudgeEntity> findJudgeWithoutPage(SearchCondition cond, JudgeSearchBean judgeSearch) {
        String hql = "from JudgeEntity ";

        Object[] values = null;
        if (judgeSearch != null) {
            hql += judgeSearch.getWhereClause();
            List<Object> paramList = judgeSearch.getParamList();
            values = paramList.toArray();
        }
        hql += cond.getOrderString();

        List<JudgeEntity> result = hibernateTemplate.find(hql, values);
        return result;
    }

    @SuppressWarnings("unchecked")
    public JudgeEntity findJudgeByIdNo(String idNo) {
        String hql = "from JudgeEntity e where e.idNo=?";
        List<JudgeEntity> result = hibernateTemplate.find(hql, idNo);
        if (result == null || result.size() == 0) {
            return null;
        } else {
            return result.get(0);
        }
    }

    @SuppressWarnings("unchecked")
    public long countJudgeByIdNo(String idNo) {
        String hql = "select count(judge.id) from JudgeEntity as judge where idNo=?";

        List<Object> result = hibernateTemplate.find(hql, idNo);

        return (Long) result.get(0);
    }
}
